package com.cmccgz.udacs_landlord_webapp.main.user.userinfo;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.udacs_landlord_webapp.bean.UserInfo;
import com.cmccgz.udacs_landlord_webapp.update.CheckUpdate;

import java.io.File;

/**
 * @author INX
 * 用户信息activity
 */
public class UserInfoActivity extends BaseActivity{
    private UserInfo userInfo;//缓冲当前服务器的userinfo
    private UserInfo userInfoEdit;//当前编辑的userinfo
    @Override
    protected int setLayoutId() {
        return 0;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initFragment() {
        Bundle savedInstanceState = getSavedInstanceState();
        if (isSaveInstance&& savedInstanceState !=null) {
            userInfo= (UserInfo) getSavedInstanceState().getSerializable("userInfo");
            userInfoEdit= (UserInfo) getSavedInstanceState().getSerializable("userInfoEdit");
        } else
            showFragment(UserInfoFragment.instance());
    }

    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    /**
     * 编辑用户信息
     */
    public void changeUserInfo(){
        UserInfoChangedFragment instance = UserInfoChangedFragment.instance();
        showFragment(instance);
    }

    /**
     * 更新用户头像
     * @param headerUrl
     */
    public void updateUserHeaderByChange(String headerUrl){
        UserInfoChangedFragment userInfoChangedFragment = (UserInfoChangedFragment) fragmentManager.findFragmentByTag(UserInfoChangedFragment.class.getSimpleName());
        if (userInfoChangedFragment!=null) {
            UserInfo userInfo=this.userInfo==null?userInfoEdit==null?null:userInfoEdit:this.userInfo;
            if (userInfo!=null&&headerUrl!=null&&new File(headerUrl).exists()) {
                (userInfoEdit=userInfo.copyNewInstance()).setHead_id(headerUrl);
            }
            userInfoChangedFragment.updateHeaderUrl(userInfoEdit);
        }
    }

    /**
     * 修改密码
     * @param data
     */
    public void changePwd(Bundle data){
        UserPwdChangedFragment instance = UserPwdChangedFragment.instance();
        if (data!=null) {
            instance.setArguments(data);
        }
        showFragment(instance);
    }

    public void checkUpdate(){
        CheckUpdate.getInstance().start(this, true);
    }

    public void updateUserInfo(){
        UserInfoFragment userInfoFragment = (UserInfoFragment) fragmentManager.findFragmentByTag(UserInfoFragment.class.getSimpleName());
        if (userInfoFragment!=null) {
            userInfoFragment.getUserInfo();
        }
    }

    /**
     * 查看用户头像
     * @param headerUrl
     */
    public void showUserHeader(String headerUrl){
        UserHeaderShowFragment instance = UserHeaderShowFragment.instance();
        Bundle args = new Bundle();
        args.putString(UserHeaderShowFragment.KEY_HEADER_URL, headerUrl);
        instance.setArguments(args);
        showFragment(instance);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment userHeaderFragment = fragmentManager.findFragmentByTag(UserHeaderShowFragment.class.getSimpleName());
        //回调给显示头像fragment
        if (userHeaderFragment!=null) {
            userHeaderFragment.onActivityResult(requestCode,resultCode,data);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putSerializable("userInfo",userInfo);
        outState.putSerializable("userInfoEdit",userInfoEdit);
        super.onSaveInstanceState(outState);
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public UserInfo getUserInfoEdit() {
        return userInfoEdit;
    }

    public void setUserInfoEdit(UserInfo userInfoEdit) {
        this.userInfoEdit = userInfoEdit;
    }

    @Override
    public void onBackPressed() {
        Fragment currentFragment = fragmentManager.findFragmentByTag(currentTag);
        if (UserInfoChangedFragment.class.getSimpleName().equals(currentTag)
                && currentFragment !=null
                &&currentFragment instanceof UserInfoChangedFragment) {
            if (((UserInfoChangedFragment) currentFragment).isNoChanged()) {
                super.onBackPressed();
            }else{
                ((UserInfoChangedFragment) currentFragment).back();
            }
        }else
            super.onBackPressed();
    }

    public void backEditUserInfo(){
        Fragment currentFragment = fragmentManager.findFragmentByTag(currentTag);
        if (UserInfoChangedFragment.class.getSimpleName().equals(currentTag)
                && currentFragment !=null
                &&currentFragment instanceof UserInfoChangedFragment)
            super.onBackPressed();
    }
}
